package com.leyou;


import com.leyou.common.auth.utils.RsaUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.junit.Test;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Date;

public class rsa {

    private static final String pub = "C:\\Users\\chenrenjie\\Desktop\\product1\\auth\\rsa_key.pub";

    private static final String pri = "C:\\Users\\chenrenjie\\Desktop\\product1\\auth\\rsa_key";

    @Test
    public void rsaTest() throws Exception {
        //这个是对 生成密钥进行的加盐加密
//        RsaUtils.generateKey(pub,pri,"heima",2048);
        //得到 私钥和公钥
        System.out.println(RsaUtils.getPrivateKey(pri));
        System.out.println(RsaUtils.getPublicKey(pub));
        //sun.security.rsa.RSAPrivateCrtKeyImpl@ffe4c262
        //Sun RSA public key, 2048 bits
        // modulus: 25710275272219577024972480452099317579386867244509852275182744
        // 84851036221658718351179574678840556310280519979461249401929340655008592
        // 21019752623407753309715861827122309236031541219194659765413194022631637
        // 16122352440984504371219373218723899048404823494651754063129921265707954
        // 71442724696518389434901250837494338851957458869901839560144867183095823
        // 64188587273064851911049613514871507180063748887473420375545002390318975
        // 78326021118141117164017481669245850651605264802637082979489550228577431
        // 77553322728467649203922387632244838333399380084567110332548087673635032
        // 7841460790661245833004754529430761178998886003663550612253
        // public exponent: 65537
    }

    //生成token
    @Test
    public void genericToken() throws Exception{
        PrivateKey privateKey = RsaUtils.getPrivateKey(pri);
        String jws = Jwts.builder().
                setSubject("Joe").      //
                signWith(privateKey). //签名 也就是加密方式
                setExpiration(new Date(new Date().getTime()+ 11111111)). //过期时间\
                claim("chen","123").
                compact();
        System.out.println(jws);
    }

    @Test
    public void parseToken() throws Exception{
        String token = "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJKb2UiLCJleHAiOjE1NzYwNzk3NjYsImNoZW4iOiIxMjMifQ.yzE-aKBhYPTr8OJisf2fySAzd436wGNPCl2uXnZgY5iDRguck2BcxiCP6n5qSYoV-X-AHYtlzeS8i8XbD-XucbyvA0A6GgK_hI0iDFy9U5K7U5BiXhAhZ0OGByUbXhxMptggrMjOBgMC8gEyckRHvoDb158A1MEDPBR0Y8QZWVu2pQYnN42X_PKkT3pJRUjUKKup40yYdHZvv3uy_d1jbA0ec7zBffGeQrIRHoWQbyyg3POCjTVROCxtWYp4gSr9ExKhZ-GLdV0xa8Dq9L-uqS96Gujl10W60tEyVJ-iRV6MuQlJwUGG9No1HXu1GcMPyPxmwAMsZzW7IRZ0Ky678A";
        //token的map对象
        PublicKey publicKey = RsaUtils.getPublicKey(pub);
        Claims claims = Jwts.parser().setSigningKey(publicKey).parseClaimsJws(token).getBody();
        System.out.println(claims.getSubject());
        System.out.println(claims.getExpiration());
        System.out.println(claims.getIssuedAt());
        System.out.println(claims.get("chen"));
    }

}
